/*********************************************************************************
 *      Copyright:  (C) 2016 Lu Zengmeng<1540999272@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  main.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/01/2016)
 *         Author:  Lu Zengmeng <1540999272@qq.com>
 *      ChangeLog:  1, Release initial version on "08/01/2016 06:45:37 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>  
#include <string.h>  
#include <errno.h> 
#include <unistd.h>  
 
#include <sys/types.h>  
#include <sys/stat.h>  
 
#include <fcntl.h>   
#include <termios.h> 
#include <pthread.h> 
 
#include "serial.h" 
#include "tcp.h" 
#include "check.h"
 
#define  DEVICE      "/dev/ttyUSB0"
#define  BUF_SIZE    10240
 
int    usrt_fd=-1;
int    sock_fd=-1;
 
void usrt_to_socket (void);
void socket_to_usrt (void);

#ifdef __arm__
#define reboot()    system("reboot -f")
#else
#define reboot()    {printf("need reboot -f \n");exit(1);}
#endif
 
void hexdump(char *buf, const int num)
{
    int i;
    for(i = 0; i < num; i++) 
    {
        printf("%02X ", buf[i]);
    }
    printf("\n");
}


/* start main */
int main(int argc,char **argv)
{
    int    ret;
    char   *dev = DEVICE;
    pthread_t sock,usrt;//,ppp;

    if(argc==2)
    {
        dev=argv[1];
    }
    printf("usage: %s port\n",argv[0]);
    usrt_fd = open_dev(dev);

    if(usrt_fd<0)
        reboot();

    set_parity(usrt_fd,8,1,'N'); // 设置串口数据位、停止位、校验位
    set_speed(usrt_fd,115200); // 设置串口波特率
 
    // ret = pthread_create(&ppp,NULL,(void *)pppThreadRunner,NULL); // 创建sock线程接收socket数据
    // if(ret < 0)
    // {
    //     perror("phread_create");
    //     reboot();
    // }


    ret = pthread_create(&sock,NULL,(void *)socket_to_usrt,NULL); // 创建sock线程接收socket数据
    if(ret < 0)
    {
        perror("phread_create");
        reboot();
    }
 
    ret = pthread_create(&usrt,NULL,(void *)usrt_to_socket,NULL); // 创建usrt线程接收串口数据
    if(ret < 0)
    {
        perror("phread_create");
         reboot();
    }
 
    pthread_join(sock,NULL); // 等待线程退出
    pthread_join(usrt,NULL);
 
    close(sock_fd);
    close(usrt_fd);
 
    printf("main exit...\n");
    reboot();
    return 0;
}
 
 
/**************************************************************************************
 *  Description:接收socket数据并通过串口转发
 *   Input Args:
 *  Output Args:
 * Return Value:
 *************************************************************************************/
void socket_to_usrt (void)
{
    printf("start socket_to_usrt...\n");
    int     n = 0;
    int     m = 0;
    char    buf[BUF_SIZE];
 
    while(1)
    {
        while(sock_fd<0)
        {
            printf("rst to connect server...\n");
            sleep(1);
            // if(cshell_netlink_status("ppp0")!=3 || cshell_netlink_status("eth0")!=3 || cshell_netlink_status("eth1")!=3)
            // {
            //     printf("wait ppp...\n");
            //     continue;
            // }

            sock_fd=socket_init();
            if(sock_fd)
            {
                // 23 2A 2A 2A 42 72 6F 61 64 63 61 73 74 41 64 64 72 65 73 73 2A 2A 2A 23
                char buf_connect[]={0x23,0x2A,0x2A,0x2A,0x42,0x72,0x6F,0x61,0x64,0x63,0x61,0x73,0x74,0x41,0x64,0x64,0x72,0x65,0x73,0x73,0x2A,0x2A,0x2A,0x23};
                write(sock_fd,buf_connect,sizeof(buf_connect));
                
            }
        }

        memset(buf,0,BUF_SIZE);
        n = read(sock_fd,buf,BUF_SIZE);
        if(n>0)
        {
            m = write(usrt_fd,buf,n);
            
            if(m<0)
            {
                perror("write to usrt");
            }
            else
            {
                printf("GET:");
                hexdump(buf, m);
            }
        }
        else 
        {
            perror("read from socket");
            sock_fd = -1;
        }
    }
    reboot();
    printf("socket_to_usrt exit...\n");
    pthread_exit(0);
} /* ----- End of sock_to_usrt()  ----- */
 
 
/**************************************************************************************
 *  Description:接收串口数据并通过socket转发
 *   Input Args:
 *  Output Args:
 * Return Value:
 *************************************************************************************/
void usrt_to_socket (void)
{
    printf("start usrt_to_socket...\n");
    int     n = 0;
    int     m = 0;
    char    buf[BUF_SIZE];
 
    while(1)
    {
        if(sock_fd<=0) continue;
        memset(buf,0,BUF_SIZE);
        n = read(usrt_fd,buf,BUF_SIZE);
        if(n>0)
        {
            m = write(sock_fd,buf,n);
            if(m<0)
            {
                perror("write to sock");
            }
            else
            {
                printf("PUT:");
                hexdump(buf, m);
            }
        }
        else if(n<0)
        {
            perror("read from usrt");
        }
    }
    
    printf("usrt_to_socket exit...\n");
    reboot();
    pthread_exit(0);
} /* ----- End of usrt_to_sock()  ----- */